Ext.ns('Wlanauth.operator');
/**
 * 操作员表单
 */
Wlanauth.operator.FormPanel = Ext.extend(Wlanauth.BaseFormPanel,{
	title:'操作员资料编辑',
	baseCls:'x-plant',
	
	constructor:function(config){
		Ext.apply(this,config);
		
		var btnAddRoleHidden = !ZZExt.ApplicationContext.actionAccessable('wlanauth.role.add');
		
		var compositefield = new Ext.form.CompositeField({
	 		fieldLabel:'<span style="color:red"> * </span>角色',
			combineErrors: false,
			allowBlank:false,
			items: [{
				xtype:'localcombo',
				name:'entity.role.id',
				xtype:'remotecombo',url:'role!getAllRoles.action',allowBlank:false,
             	root:'rows',
             	fields:['name','id']
			},{
				xtype:'button',
				text:'查看',
				handler:this.viewRole,
				scope:this
			},{
				xtype:'button',
				text:'新建',
				iconCls:'add',
				hidden:btnAddRoleHidden,
				handler:function(){
					ZZExt.ApplicationContext.openTab('wlanauth.role.managerpanel',{});
				}
			}]
	 	});
		
	 	Wlanauth.operator.FormPanel.superclass.constructor.call(this,{
			actionClass:'operator',
			
			waitMsgTarget: true,
	        padding:5,
	        defaults:{xtype:'textfield',allowBlank:true},
	        items:[{
	        	fieldLabel:'用户名',
	        	name:'entity.username',
	        	allowBlank:false,
	        	width:250,
	        	xtype:'keytextfield',
	        	url:'operator!list.action?dataOnly=true'
	        },{
	        	fieldLabel:'密码',
	        	name:'entity.password',
	        	inputType:'password',
	        	allowBlank:false,
	        	width:250
	        },{
	        	fieldLabel:'重复密码',
	        	name:'password2',
	        	inputType:'password',
	        	allowBlank:false,
	        	width:250
	        },compositefield,{
	        	fieldLabel:'姓名',
	        	name:'entity.displayname',
	        	width:250
	        },{
	        	fieldLabel:'联系电话',
	        	name:'entity.contactPhone',
	        	width:250
	        },{
	        	fieldLabel:'邮箱',
	        	name:'entity.email',
	        	vtype:'email',
	        	anchor:'95%'
	        },{
	        	fieldLabel:'地址',
	        	name:'entity.address',
	        	anchor:'95%'	        	
	        },{
	        	fieldLabel:'备注',
	        	name:'entity.description',
	        	xtype:'textarea',
	        	anchor:'95%'
	        },{
	        	xtype:'hidden',
	        	name:'entity.id'
	        }],
	        buttons:[{
	        	text:'保存',
	        	iconCls:'edit',
	        	handler:this.doSubmit,
	        	scope:this
	        },{
	        	text:'取消',
	        	iconCls:'cancel',
	        	handler:function(){
	        		ZZExt.ApplicationContext.removeTabCmp(this);
	        	},
	        	scope:this
	        }]
     	});
	 	this.on('actionSuccess',function(){
	 		var _this = this;
	 		var msg = '操作成功,数据已保存! 是否继续添加?';
	 		if(_this.editMode =='update'){
	 			msg = '操作成功,数据已保存! 是否继续修改?'
	 		}
	 		Ext.Msg.confirm('提示',msg,function(btn){
	 			if(btn=='yes'){
	 				if(_this.editMode !='update'){
	 					_this.getForm().reset();
	 				}
	 			} else {
	 				ZZExt.ApplicationContext.removeTabCmp(_this);
	 				var tab = ZZExt.ApplicationContext.openTab('wlanauth.operator.gridpanel');
	 				tab.getStore().reload();
	 			}
	 		});
	 	  
	 	},this);
	 	
	 	this.on('ZZExt.init',function(params){
	 		if(params){
		 		var id = params.id;
		 		this.loadDataById(id);
	 		}
	 	});
	},
	isValid:function(form){
		var flag = form.isValid();
		if(!flag) return false;
		var pwd1 = form.findField('entity.password').getValue();
		var pwd2 = form.findField('password2').getValue();
		if(pwd1!=pwd2){
			Ext.Msg.alert('提示','两次输入的密码不一致');
			return false;
		}
		return flag;
	},
	viewRole:function(){
		var roleId = this.getForm().findField('entity.role.id').getValue();
		if(roleId==''){
			Ext.Msg.alert('提示','未选择任何角色');
			return ;
		}
		var win = new Ext.Window({
			title:'权限查看',
			height:350,
			width:480,
			resizable:false,
			autoScroll:true,
			closeAction:'close',
			items:new Wlanauth.role.FormPanel({
				listeners:{
					'resourceComponentsInited':function(formpanel){
						formpanel.loadDataById(roleId);
					}
				}
			})
		});
		win.show();
	},
	doSubmit:function(){
      	var _url = this.actionClass+'!save.action';
      	if(this.editMode.toUpperCase() == 'UPDATE')
      		_url = 'updateOperator.action';
      	var _form = this.getForm();
      	if(this.isValid(_form)){
      		_form.submit({
      			url:_url,
      			scope:this,
      			waitMsg:'正在保存数据...',
      			success:function(form,action){
      				this.fireEvent('actionSuccess',form,action);
      			}
      		});
      	}
      }
});

Ext.reg('wlanauth.operator.formpanel',Wlanauth.operator.FormPanel);